﻿using Models;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DAL
{
    public class D_config
    {
        public List<Sys_config> list_config()
        {
            //单表分页
            //var list = Sugar_helper.Db.Queryable<configs>().ToPageList(pagenumber, pageSize, ref totalCount);

            var list = Sugar_helper.Db.Queryable<Sys_config>().ToList();// 使用SqlSugar的Queryable方法查询Sys_config表的所有记录，并将结果转换为List<Sys_config>类型。
            return list;
        }

        public int add_list(List<Sys_config> configs)
        {
            var res = Sugar_helper.Db.Insertable(configs).ExecuteCommand();
            return res;
        }
        public bool Del(List<Sys_config> list)
        {
            var res = Sugar_helper.Db.Deleteable(list).ExecuteCommand();
            return res == list.Count? true: false;
        }

        public bool save_List(List<Sys_config> sys_Configs)
        {
            var count = Sugar_helper.Db.Storageable<Sys_config>(sys_Configs).ExecuteCommand();
            return count == sys_Configs.Count ? true : false;
        }


        public bool Teacher_Timeout()
        {
            var res = Sugar_helper.Db.Ado.SqlQuery<Sys_config>("SELECT * FROM Sys_config WHERE teacher_deadline < GETDATE()");
            return res.Count ==1 ?true : false;
        }

        public bool Stu_Timeout()
        {
            var res = Sugar_helper.Db.Ado.SqlQuery<Sys_config>($"SELECT * FROM Sys_config WHERE stu_deadline < GETDATE()");
            return res.Count == 1 ? true : false;
        }
        public bool Stu_Timeout(Student student)
        {
            var res = Sugar_helper.Db.Ado.SqlQuery<Sys_config>($"SELECT * FROM Sys_config WHERE stu_deadline < GETDATE() and major_id={student.major_id}");
            return res.Count == 1 ? true : false;
        }
        public List<Sys_config> Teacher_Timeout(Teacher teacher)
        {
            var res = Sugar_helper.Db.Queryable<Sys_config>().Where(conf => SqlFunc.Subqueryable<Tutor>()
                                                            .Where(T=>T.teacher_id==teacher.teacher_id).Any()).ToList();

            return res;
        }

    }
}
